package com.itboye.sunsun;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.custome.RoundImage;
import com.itboye.sunsun.person.ui.NewsDetailShareFragment;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.person.ui.SttingActivity;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsunhome.R;

import static com.itboye.sunsun.constants.NetPublicConstant.WEB_URL;

@SuppressLint("ValidFragment")
public class MeFragment extends BaseFragment implements OnClickListener {

    private Context context;
    private View meView;

    private RoundImage roundview;
    private TextView tvname, tvaq, share;
    private RelativeLayout personsettingrela, mycollectrela,
            mylocationrelaMessge, realypoutTop, mywordsrela;
    private LinearLayout linyoutTop;
    private PopupWindow popupWindow;

    public MeFragment() {
    }

    public MeFragment(Context context) {
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        meView = inflater.inflate(R.layout.fragment_me, container, false);
        smartInject(meView);

        initMobile();

        // 注册广播
        IntentFilter intentFilterAvart = new IntentFilter("changeavart");
        getActivity().registerReceiver(avartBroadcastReceiver,
                intentFilterAvart);


        IntentFilter intentFilterAvartnecheng = new IntentFilter("necheng");
        getActivity().registerReceiver(avartBroadcastReceivernehneg,
                intentFilterAvartnecheng);

        String uri = (String) SPUtils.get(App.ctx, null, SPContants.AVATAR_URL,
                "");
        System.out.println();
        XImageLoader.load(uri, roundview);
        return meView;
    }

    public void onClick(View v) {

        switch (v.getId()) {
            // case R.id.viewallrela:
            //
            // LoginController.onAllOrderClick(getActivity(), null);
            // break;
            case R.id.payrela:
                LoginController.onPayClick(getActivity(), null);
                break;
            case R.id.linyoutTop:
                LoginController.onPersonalsettingClick(getActivity(), null);
                break;
            case R.id.realypoutTop:
                LoginController.onPersonalsettingClick(getActivity(), null);
                break;
//            case R.id.mylocationrela:
//                LoginController.onMyLocationClick(getActivity(), null);
//                break;
            case R.id.mycollectrela:
                LoginController.onMyCollectClick(getActivity(), null);
                break;
            case R.id.personsettingrela:
                Intent intent = new Intent(getActivity(), SttingActivity.class);
                startActivity(intent);
                break;
            case R.id.mylocationrelaMessge:
                LoginController.onPointMarketClick(getActivity(), null);
                break;
            case R.id.mywordsrela:
                LoginController.onMyWordsClick(getActivity(), null);
                break;
            case R.id.roundview:
                LoginController.onPersonalsettingClick(getActivity(), null);
                break;
            case R.id.share:
                showSharePop(v);
                break;
            default:
                break;
        }

    }

    NewsDetailShareFragment fragment;

    private void showSharePop(View v) {
        fragment = new NewsDetailShareFragment();
        Bundle args = new Bundle();
        args.putString("content", getString(R.string.download));
        args.putString("title", getString(R.string.download));
        fragment.setArguments(args);
        //APP_URL/User/download?type=XX&id=XX
//        其中type暂时输入'product'或者'thread'，id为其id
//                type=sunsunhome就是下载森森之家
        fragment.show(getActivity().getSupportFragmentManager(),
                WEB_URL + "/User/download?type=sunsunhome");
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 注册广播
        IntentFilter intentFilterAvart = new IntentFilter(
                PersonSettingActivity.AVARTACTION);
        getActivity().registerReceiver(avartBroadcastReceiver,
                intentFilterAvart);

        IntentFilter intentFilterExit = new IntentFilter(
                PersonSettingActivity.EXITCHANGE);
        getActivity()
                .registerReceiver(avartBroadcastReceiver, intentFilterExit);

    }

    ;

    BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String resultData = (String) SPUtils.get(App.ctx, null,
                    SPContants.AVATAR_URL, "");

            initMobile();
            XImageLoader.load(resultData, roundview);
        }

        ;
    };


    BroadcastReceiver avartBroadcastReceivernehneg = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String resultData = (String) SPUtils.get(App.ctx, null,
                    SPContants.NICK, "");

            initMobile();
            tvname.setText(resultData);
        }

        ;
    };

    public void initMobile() {


        String phonenumber = (String) SPUtils.get(App.ctx, null,
                SPContants.NICK, "");
        String AQ_num = (String) SPUtils.get(App.ctx, null, SPContants.AQ_NUM,
                "");
        tvname.setText("" + phonenumber);
        if (AQ_num.equals("")) {
            tvaq.setText("0");
        } else

            tvaq.setText(AQ_num);

    }

}
